﻿using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.OS;
using Android.Widget;
using AndroidX.Core.App;
using AndroidX.Core.Content;
using Huawei.Hms.Hmsscankit;
using Huawei.Hms.Ml.Scan;

using Microsoft.AspNetCore.Components;

namespace HuaweiBarcodeScan
{
    [Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
    public class MainActivity : MauiAppCompatActivity
    {
        private const int REQUEST_CLASSIC_CODE_SCAN = 0x02;//默认扫码
        private const int DEFINED_CODE = 222;//当用于接收授权结果时判断是不是自己请求的
        private EventCallback<string>? ScanResultCallback;

        public void LaunchScanActivity(EventCallback<string> eventCallback)
        {
            ScanResultCallback = eventCallback;
            //检查权限并启动扫码
            if (CheckPermission(new string[] { Android.Manifest.Permission.Camera, Android.Manifest.Permission.ReadMediaImages }, DEFINED_CODE))
            {
                StartScan(this, REQUEST_CLASSIC_CODE_SCAN, new HmsScanAnalyzerOptions.Creator()
                    .SetHmsScanTypes(HmsScan.AllScanType)
                    .SetPhotoMode(true)
                    .Create());
            }
        }
        private bool CheckPermission(string[] permissions, int requestCode)
        {
            var hasAllPermissions = true;
            foreach (string permission in permissions)
            {
                if (ContextCompat.CheckSelfPermission(this, permission) == Permission.Denied)
                {
                    hasAllPermissions = false;
                    ActivityCompat.RequestPermissions(this, permissions, requestCode);
                }
            }

            return hasAllPermissions;
        }
        /// <summary>
        /// 启动扫码，启动前应检查是否有相机权限
        /// </summary>
        /// <param name="activity"></param>
        /// <param name="request_code"></param>
        /// <param name="ops"></param>
        private void StartScan(Activity activity, int request_code, HmsScanAnalyzerOptions ops)
        {
            ScanUtil.StartScan(activity, request_code, ops);
        }
        protected override void OnActivityResult(int requestCode, Result resultCode, Intent? data)
        {
            base.OnActivityResult(requestCode, resultCode, data);
            if (resultCode != Result.Ok || data == null)
            {
                Android.Widget.Toast.MakeText(this, "结果异常", ToastLength.Short)?.Show();
                return;
            }
            HmsScan? hmsScan;
            switch (requestCode)
            {

                case REQUEST_CLASSIC_CODE_SCAN:
                    hmsScan = null;
                    if (data.Extras != null)
                    {
                        hmsScan = data.Extras.Get(ScanUtil.Result) as HmsScan;
                    }
                    break;
                default:
                    hmsScan = null;
                    break;
            }
            if (hmsScan != null && !string.IsNullOrWhiteSpace(hmsScan.OriginalValue))
            {
                // 将结果传递给 回调函数处理
                ScanResultCallback?.InvokeAsync(hmsScan.OriginalValue);
            }
        }
        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
        {
            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
            bool hasAllPermissions = true;
            for (int i = 0; i < permissions.Length; i++)
            {
                if (grantResults[i] == Permission.Denied)
                {
                    hasAllPermissions = false;
                    break;
                }
            }
            if (hasAllPermissions)
            {
                if (requestCode == DEFINED_CODE)
                {
                    StartScan(this, REQUEST_CLASSIC_CODE_SCAN, new HmsScanAnalyzerOptions.Creator()
                    .SetHmsScanTypes(HmsScan.AllScanType)
                    .SetPhotoMode(true)
                    .Create());
                }
            }
        }
    }
}
